package HeSuanServer.handlerInterceptor;

import HeSuanServer.uitl.JwtUtil;
import com.auth0.jwt.interfaces.Claim;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @描述:
 * @author: LH
 * @date: 2022/12/8
 */
//@Component
@Slf4j
public class TokenHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("token拦截");
        String token = request.getHeader("authorization");
        log.info("token:"+token);
        try{
            Map<String, Claim> stringClaimMap = JwtUtil.verifyJWTToken(token);
            log.info("token验证通过");
            return false;
        }catch (Exception e) {
            response.sendRedirect("\"http://localhost:8080/#/login");
            log.info("token验证未通过");
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
